package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.Goods;
import factory.ServiceFactory;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class List
 */
@WebServlet("/list.do")
public class List extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public List() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Date date = (Date)request.getServletContext().getAttribute("date");
		Date nDate = new Date(System.currentTimeMillis());
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(nDate);
	    Calendar cal2 = Calendar.getInstance();  
		if(date != null)
			cal2.setTime(date);
		if(date == null || cal1.get(Calendar.MONTH) != cal2.get(Calendar.MONTH) || cal1.get(Calendar.DATE) != cal2.get(Calendar.DATE) ) {
			request.getServletContext().setAttribute("date", nDate);
			ServiceFactory.getSerInstance().caculatePreMonth();
		}
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		String pagestring = request.getParameter("page");
		int page = 1;
		if (!(pagestring == null || pagestring.equals("")))
			page = Integer.valueOf(pagestring);
		long count = ServiceFactory.getSerInstance().countGoods();
		int all = (int) count / 20;
		if (count % 20 > 0) {
			all++;
		}
		if (all == 0)
			all = 1;
		if (page < 0 || (page - 1) * 20 > count) {
			JSONObject json = new JSONObject();
			json.put("status", 500);
			json.put("count", all);
			json.put("msg", "页数超出范围！");
			out.print(json);
		} else {
			java.util.List<Goods> goods = ServiceFactory.getSerInstance().getTwntyGoods(page);
			String json = "{\"status\":200,\"count\":" + all + ",\"content\":[";
			for (int i = 0, l = goods.size(); i < l; i++) {
				if (i == 0) {
					json += "{" + "\"txm\":\"" + goods.get(i).getTxm() + "\"," + "\"name\":\"" + goods.get(i).getName()
							+ "\"," + "\"sl\":\"" + goods.get(i).getNumber() + "\"," + "\"month\":\""+goods.get(i).getMonth()+"\"}";
				} else {
					json += ",{" + "\"txm\":\"" + goods.get(i).getTxm() + "\"," + "\"name\":\"" + goods.get(i).getName()
							+ "\"," + "\"sl\":\"" + goods.get(i).getNumber() + "\"," + "\"month\":\""+goods.get(i).getMonth()+"\"}";
				}
			}
			json += "]}";
			out.println(json);
		}
		out.close();
	}

}
